package com.ruoyi.yzchu.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.vo.ArticleVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yzchu.domain.YzcArticle;
import com.ruoyi.yzchu.service.IYzcArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-06
 */



// 调整基础路径
@RestController
@RequestMapping("/system/article") // 原路径/system/article改为模块化路径
public class YzcArticleController extends BaseController {
    // ...原有其他方法保持不变...
//}

//@RestController
//@RequestMapping("/system/article")
//public class YzcArticleController extends BaseController
//{
    @Autowired
    private IYzcArticleService yzcArticleService;

    // 新增接口路径
    @Anonymous
    @GetMapping("/detail/{articleId}")
    public AjaxResult getDetail(@PathVariable Long articleId) {
        return success(yzcArticleService.getArticleDetail(articleId));
    }



    // YzcArticleController.java

    /**
     * 原后台管理接口（保持不变）
     * 路径：/system/article/list
     */
//    @Anonymous
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo adminList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            YzcArticle yzcArticle
    ) {
        startPage();
        List<YzcArticle> list = yzcArticleService.selectYzcArticleList(yzcArticle);
        return getDataTable(list);
    }

    /**
     * 新增APP端接口（带用户信息）
     * 路径：/system/article/appList
     */
    @Anonymous
    @GetMapping("/applist")
    public TableDataInfo appList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            YzcArticle yzcArticle
    ) {
        startPage(); // 使用相同分页方式
        List<ArticleVO> list = yzcArticleService.selectArticleListWithUser(yzcArticle);
        return getDataTable(list);
    }

//    private void startPage(Integer pageNum, Integer pageSize) {
//    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YzcArticle yzcArticle)
    {
        List<YzcArticle> list = yzcArticleService.selectYzcArticleList(yzcArticle);
        ExcelUtil<YzcArticle> util = new ExcelUtil<YzcArticle>(YzcArticle.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(yzcArticleService.selectYzcArticleByArticleId(articleId));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YzcArticle yzcArticle)
    {
        // URL解码
        try {
            String decodedContent = URLDecoder.decode(yzcArticle.getContent(), "UTF-8");
            yzcArticle.setContent(decodedContent);
            return toAjax(yzcArticleService.insertYzcArticle(yzcArticle));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 encoding not supported", e);
        }
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YzcArticle yzcArticle)
    {

        // URL解码
        try {
            String decodedContent = URLDecoder.decode(yzcArticle.getContent(), "UTF-8");
            yzcArticle.setContent(decodedContent);
            return toAjax(yzcArticleService.updateYzcArticle(yzcArticle));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 encoding not supported", e);
        }

    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(yzcArticleService.deleteYzcArticleByArticleIds(articleIds));
    }
}
